package cn.web.management.service.impl;

import cn.web.management.dao.inface.UpdateUserDao;
import cn.web.management.dao.impl.UpdateUserDaoImpl;
import cn.web.management.service.inface.RegisteredUser;
import cn.web.management.utils.JDBCUtil;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Map;

public class RegisteredUserImpl implements RegisteredUser {
    private UpdateUserDao updateUserDao = new UpdateUserDaoImpl();
    private JdbcTemplate template = new JdbcTemplate(JDBCUtil.getDataSource());

    /**
     * @description: 注册新用户，为其分配工号
     * @paramName: [username, password]
     * @paramType: [java.lang.String, java.lang.String]
     * @returnType: int
     */
    public int addUser(String username, String password){
        String employno = "";
        String temp = "";
        String s = "";
        String sql = "select employno from personal_inform order by employno DESC limit 0,1";
        Map<String, Object> stringObjectMap = template.queryForMap(sql);

        temp = (String) stringObjectMap.get("employno");
        for (int i = 0; i < temp.length(); i++) {
            if(i!=0)
                s+=temp.charAt(i);
        }


        int num = Integer.valueOf(s);
        num+=1;
        employno = "E";
        for (int i = 0; i < 5-(num+"").length(); i++) {
            employno+=0;
        }
        employno += num;

        return updateUserDao.addUser(employno,username,password);
    }
}
